package com.xqx.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xqx.domain.User;
import com.xqx.domain.ValueObject;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import sun.awt.SunHints;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    // 请求地址  http://localhost:8082/xqx_spring_mvc/user/quick?username=xqx
    @RequestMapping(value = "/quick", method = RequestMethod.GET, params = {"username"})
    public String save() {
        System.out.println("Controller save running....");
        return "success";
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quickUser
     * @param user
     * @return
     */
    @RequestMapping(value = "/quickUser")
    public String save(User user) {
        System.out.println(user);
        return "success";
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quickUserRedirect
     * @param user
     * @return
     */
    @RequestMapping(value = "/quickUserRedirect")
    public String saveRedirect(User user) {
        System.out.println(user);
        //重定向
        return "redirect:/jsp/success.jsp";
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quickUsername
     * @return
     */
    @RequestMapping("/quickUsername")
    public ModelAndView quick() {
        ModelAndView modelAndView = new ModelAndView();
        //设置返回数据
        modelAndView.addObject("username", "xingquanxiang");
        //设置跳转视图
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick1
     * @param modelAndView
     * @return
     */
    @RequestMapping("/quick1")
    public ModelAndView quick1(ModelAndView modelAndView){
        //设置返回数据
        modelAndView.addObject("username", "zhangyaxin");
        //设置跳转视图
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick2
     * @param model
     * @return
     */
    @RequestMapping("/quick2")
    public String quick2(Model model){
        //设置返回数据
        model.addAttribute("username", "hyh");
        //设置跳转视图
        return "success";
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick3
     * @param request
     * @return
     */
    @RequestMapping("/quick3")
    public String quick3(HttpServletRequest request){
        //设置返回数据
        request.setAttribute("username", "yq");
        //设置跳转视图
        return "success";
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick4?age=21
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/quick4")
    public void quick4(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //设置返回数据
        String age = request.getParameter("age");
        //将数据回写到网页
        response.getWriter().write(age);
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick5?age=21
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/quick5")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public String quick5(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String age = request.getParameter("age");
        //将数据回写到网页
        return age;
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick6
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("/quick6")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public String quick6() throws JsonProcessingException {
        User user = new User();
        user.setUsername("xqx");
        user.setAge(21);
        //将对象转换为json数据格式
        String userJson = new ObjectMapper().writeValueAsString(user);
        return userJson;
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick7
     * 将对象以json数据格式响应到客户端
     * 配置SpringMVC注解驱动 默认底层就会集成jackson进行对象或集合的json格式字符串的转换
     *     <mvc:annotation-driven/>
     * @return
     */
    @RequestMapping("/quick7")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public User quick7(){
        User user = new User();
        user.setUsername("xqx");
        user.setAge(22);
        return user;
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick8?username=xqx&age=18
     * @param username
     * @param age
     */
    @RequestMapping("/quick8")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick8(String username, int age){
        System.out.println(username+":"+age);
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick9?username=xqx&age=18
     * http://localhost:8082/xqx_spring_mvc/user/quick9?username=xqx&age=18&hobby=%E7%AF%AE%E7%90%83&hobby=coding
     * @param user
     * @param hobby
     */
    @RequestMapping("/quick9")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick9(User user,String[] hobby){
        System.out.println(user);
        System.out.println("爱好：");
        if(hobby!=null) {
            for (String s : hobby) {
                System.out.print(s+",");
            }
        }
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/form.jsp
     * @param vo
     */
    @RequestMapping("/quick10")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick10(ValueObject vo){
        System.out.println(vo);
    }

    /**
     *
     * @param userList
     */
    @RequestMapping("/quick11")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick11(@RequestBody List<User> userList){
        System.out.println(userList);
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/userForm.jsp
     * @param user
     */
    @RequestMapping("/quick12")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick12(User user){
        System.out.println(user);
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick13
     * http://localhost:8082/xqx_spring_mvc/user/quick13?name=倖权祥
     * http://localhost:8082/xqx_spring_mvc/user/quick13?username=倖权祥
     *
     * 参数绑定注解
     * @param username
     */
    @RequestMapping("/quick13")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    /**
     * @RequestParam(value = "name", required = false, defaultValue = "xqx")
     * value:与请求参数名称相同
     * required:此在指定的请求参数是否必须包含，默认是true，提交时如果没有此参数则报错
     * defaultValue：当没有指定的请求参数时，则使用指定的默认值赋值
     */
    public void quick13(@RequestParam(value = "name", required = false, defaultValue = "xqx") String username){
        System.out.println(username);
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick14/zhansan
     * @param username
     */
    @RequestMapping("/quick14/{username}")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick14(@PathVariable(value = "username", required = false) String username){
        System.out.println(username);
    }


    /**
     * 默认：http://localhost:8082/xqx_spring_mvc/user/quick15?date=2019/10/1
     * 自定义类型转换器
     * http://localhost:8082/xqx_spring_mvc/user/quick15?date=2019-10-1
     * @param date
     */
    @RequestMapping("/quick15")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick15(Date date){
        System.out.println(date);
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick16
     * 获得servlet相关API
     * @param request
     * @param response
     * @param session
     */
    @RequestMapping("/quick16")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick16(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println("request = " + request);
        System.out.println("response = " + response);
        System.out.println("session = " + session);
    }

    /**
     * 获取请求头
     * http://localhost:8082/xqx_spring_mvc/user/quick17
     * @param user_agent
     */
    @RequestMapping("/quick17")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick17(@RequestHeader(value = "User-Agent", required = false) String user_agent){
        System.out.println("user_agent = " + user_agent);
    }

    /**
     * http://localhost:8082/xqx_spring_mvc/user/quick18
     * 获取cookie数据
     * @param jsessionID
     */
    @RequestMapping("/quick18")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick18(@CookieValue(value = "JSESSIONID", required = false) String jsessionID){
        System.out.println("jsessionID = " + jsessionID);
    }

    /**
     * 单文件上传
     * http://localhost:8082/xqx_spring_mvc/upload.jsp
     * @param name
     * @param uploadFile
     * @throws IOException
     */
    @RequestMapping("/quick19")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick19(@RequestParam(value = "username") String name, MultipartFile uploadFile, HttpServletRequest request) throws IOException {
        System.out.println("name = " + name);
        System.out.println("uploadFile = " + uploadFile);
        String realPath = request.getRealPath("/upload/");
        //获取上传文件名称
        String originalFilename = uploadFile.getOriginalFilename();

        //创建文件
        File file = new File( realPath + originalFilename);
        //执行上传
        uploadFile.transferTo(file);
        System.out.println(file.getAbsolutePath());

    }

    /**
     * http://localhost:8082/xqx_spring_mvc/upload.jsp
     * 多文件上传
     */
    @RequestMapping("/quick20")
    //告知springMVC框架，不进行页面跳转，直接进行数据响应
    @ResponseBody
    public void quick20(String username, MultipartFile[] uploadFile) throws IOException {
        System.out.println("username = " + username);
        System.out.println("uploadFile = " + uploadFile);
        for (MultipartFile file : uploadFile) {
            //获取上传文件名称
            String originalFilename = file.getOriginalFilename();
            //创建文件
            File _file = new File("G:\\DarkHorseCode\\ssm\\Spring\\xqx_spring_mvc\\src\\main\\webapp\\upload\\" + originalFilename);
            //执行上传
            file.transferTo(_file);
            System.out.println(_file.getAbsolutePath());
        }
    }

}
